{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Keyboard Row"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findWords"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #键盘行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串数组 <code>words</code> ，只返回可以使用在 <strong>美式键盘</strong> 同一行的字母打印出来的单词。键盘如下图所示。</p>\n",
    "\n",
    "<p><strong>美式键盘</strong> 中：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>第一行由字符 <code>\"qwertyuiop\"</code> 组成。</li>\n",
    "\t<li>第二行由字符 <code>\"asdfghjkl\"</code> 组成。</li>\n",
    "\t<li>第三行由字符 <code>\"zxcvbnm\"</code> 组成。</li>\n",
    "</ul>\n",
    "\n",
    "<p><img alt=\"American keyboard\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/keyboard.png\" style=\"width: 100%; max-width: 600px\" /></p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>words = [\"Hello\",\"Alaska\",\"Dad\",\"Peace\"]\n",
    "<strong>输出：</strong>[\"Alaska\",\"Dad\"]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>words = [\"omk\"]\n",
    "<strong>输出：</strong>[]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>words = [\"adsdf\",\"sfd\"]\n",
    "<strong>输出：</strong>[\"adsdf\",\"sfd\"]\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= words.length <= 20</code></li>\n",
    "\t<li><code>1 <= words[i].length <= 100</code></li>\n",
    "\t<li><code>words[i]</code> 由英文字母（小写和大写字母）组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [keyboard-row](https://leetcode.cn/problems/keyboard-row/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [keyboard-row](https://leetcode.cn/problems/keyboard-row/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"Hello\",\"Alaska\",\"Dad\",\"Peace\"]', '[\"omk\"]', '[\"adsdf\",\"sfd\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        keyboard=['qwertyuiop','asdfghjkl','zxcvbnm']\n",
    "        ans=[]\n",
    "        for word in words:\n",
    "            word_=word.lower()\n",
    "            if word_[0] in keyboard[0]:\n",
    "                tem=0\n",
    "            elif word_[0] in keyboard[1]:\n",
    "                tem=1\n",
    "            else:\n",
    "                tem=2\n",
    "            for i in range(len(word)):\n",
    "                if (i==len(word)-1) and (word_[i] in keyboard[tem]):\n",
    "                    ans.append(word)\n",
    "                if word_[i] not in keyboard[tem]:\n",
    "                    break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        row1='qwertyuiopQWERTYUIOP'\n",
    "        row2='asdfghjklASDFGHJKL'\n",
    "        row3='zxcvbnmZXCVBNM'\n",
    "        result = []\n",
    "        for word in words:\n",
    "          is_row1_word = all(char in row1 for char in word)\n",
    "          is_row2_word = all(char in row2 for char in word)\n",
    "          is_row3_word = all(char in row3 for char in word)\n",
    "          if is_row1_word or is_row2_word or is_row3_word:\n",
    "            result.append(word)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        set1 = set(\"qwertyuiopQWERTYUIOP\")\n",
    "        set2 = set(\"asdfghjklASDFGHJKL\")\n",
    "        set3 = set(\"zxcvbnmZXCVBNM\")\n",
    "        for word in words:\n",
    "            st = set()\n",
    "            for c in word:\n",
    "                if c in set1: st.add(1)\n",
    "                elif c in set2: st.add(2)\n",
    "                else: st.add(3)\n",
    "            if len(st) == 1:\n",
    "                ans.append(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        lst=[]\n",
    "        for i in words:\n",
    "            j=i.lower()\n",
    "            if j.strip('qwertyuiop')=='' or j.strip('asdfghjkl')=='' or j.strip('zxcvbnm')=='':\n",
    "                lst.append(i)\n",
    "        return lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        row = ['qwertyuiopQWERTYUIOP','asdfghjklASDFGHJKL','zxcvbnmZXCVBNM']\n",
    "        res = []\n",
    "        for i in range(len(words)):\n",
    "            flag = True\n",
    "            s = words[i]\n",
    "            if s[0] in row[0]:\n",
    "                ind =0\n",
    "            elif s[0] in row[1]:\n",
    "                ind = 1\n",
    "            else:\n",
    "                ind = 2\n",
    "            for j in range(1,len(s)):\n",
    "                if s[j] not in row[ind]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag == True:\n",
    "                res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        set1 = set('qwertyuiop')\n",
    "        set2 = set('asdfghjkl')\n",
    "        set3 = set('zxcvbnm')\n",
    "        res = []\n",
    "        for i in words:\n",
    "            x = i.lower()\n",
    "            setx = set(x)\n",
    "            if setx<=set1 or setx<=set2 or setx<=set3:\n",
    "                res.append(i)\n",
    "        return res\n",
    "# 亮点setx<=set1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        cl1 = set(\"qwertyuiop\")\n",
    "        cl2 = set(\"asdfghjkl\")\n",
    "        cl3 = set(\"zxcvbnm\")\n",
    "        new_word = []\n",
    "        for each in words:\n",
    "            s = each.lower()\n",
    "            word = set(s)\n",
    "            if word.issubset(cl1) or word.issubset(cl2) or word.issubset(cl3):\n",
    "                new_word.append(each)\n",
    "        \n",
    "        return new_word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        r01 = \"qwertyuiop\"\n",
    "        r02 = \"asdfghjkl\"\n",
    "        r03 = \"zxcvbnm\"\n",
    "        ret = list()\n",
    "        for i in words:\n",
    "            rn = 0\n",
    "            f = 1\n",
    "            k = i.lower()\n",
    "            for j in k:\n",
    "                if j in r01:\n",
    "                    if rn == 1 or rn ==0 :\n",
    "                         rn =1\n",
    "                         continue\n",
    "                    else:\n",
    "                        f=0\n",
    "                        break\n",
    "                if j in r02:\n",
    "                    if rn == 2 or rn ==0 :\n",
    "                         rn =2\n",
    "                         continue\n",
    "                    else:\n",
    "                        f=0\n",
    "                        break\n",
    "                if j in r03:\n",
    "                    if rn == 3 or rn ==0 :\n",
    "                         rn =3\n",
    "                         continue\n",
    "                    else:\n",
    "                        f=0\n",
    "                        break\n",
    "            if f:ret.append(i)\n",
    "        return ret\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        res=[]\n",
    "        rowindex='12210111011122000010020202'\n",
    "        for word in words:\n",
    "            flag=rowindex[ord(word.lower()[0])-ord('a')]\n",
    "            if all(rowindex[ord(j)-ord('a')]==flag for j in word.lower()):\n",
    "                res.append(word)\n",
    "        return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        u = list(\"qwertyuiop\")\n",
    "        m = list(\"asdfghjkl\")\n",
    "        d = list(\"zxcvbnm\")\n",
    "        return [i for i in words if (len(i) == len([_ for _ in i if _.lower() in u])) or (len(i) == len([_ for _ in i if _.lower() in m])) or (len(i) == len([_ for _ in i if _.lower() in d]))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        ret = []\n",
    "        for i in words:\n",
    "            each = i.lower()\n",
    "            if each.strip(\"qwertyuiop\") == \"\" or each.strip(\"asdfghjkl\") == \"\" or each.strip(\"zxcvbnm\") == \"\":\n",
    "                ret.append(i)\n",
    "        return ret\n",
    "\n",
    "                \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        lst = ['qwertyuiop','asdfghjkl','zxcvbnm']\n",
    "        dict ={}\n",
    "\n",
    "        for x in range(len(lst)):\n",
    "            for y in lst[x]:\n",
    "                dict[y] = x\n",
    "        res =[]\n",
    "        for i in words:\n",
    "            ans =[]\n",
    "            for j in set(i.lower()):\n",
    "                ans.append(dict[j])\n",
    "            if len(set(ans)) == 1:\n",
    "                res.append(i)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        hashmap = '12210111011122000010020202'\n",
    "        res = []\n",
    "        for word in words:\n",
    "            idx = hashmap[ord(word[0].lower()) - ord('a')]\n",
    "            if all(hashmap[ord(w.lower()) - ord('a')] == idx for w in word):\n",
    "                res.append(word)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        rowIdx = \"12210111011122000010020202\"\n",
    "        for word in words:\n",
    "            idx = rowIdx[ord(word[0].lower()) - ord('a')]\n",
    "            if all(rowIdx[ord(ch.lower()) - ord('a')] == idx for ch in word):\n",
    "                ans.append(word)\n",
    "        return ans\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        keys = [\"qwertyuiop\",\"asdfghjkl\",\"zxcvbnm\"]\n",
    "        re = []\n",
    "        tmp = -1\n",
    "        for word in words:\n",
    "            for s in word:\n",
    "                if tmp < 0 :\n",
    "                    if s in \"qwertyuiop\" or s in \"qwertyuiop\".upper():\n",
    "                        tmp = 0\n",
    "                    elif s in \"asdfghjkl\" or s in \"asdfghjkl\".upper():\n",
    "                        tmp = 1\n",
    "                    else:\n",
    "                        tmp = 2\n",
    "                elif s not in keys[tmp] and s not in keys[tmp].upper():\n",
    "                    tmp = -1\n",
    "                    break\n",
    "            else:\n",
    "                tmp = -1\n",
    "                re.append(word)\n",
    "        return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        r01 = \"qwertyuiop\"\n",
    "        r02 = \"asdfghjkl\"\n",
    "        r03 = \"zxcvbnm\"\n",
    "        ret = list()\n",
    "        for i in words:\n",
    "            rn = 0\n",
    "            f = 1\n",
    "            k = i.lower()\n",
    "            for j in k:\n",
    "                if j in r01:\n",
    "                    if rn == 1 or rn ==0 :\n",
    "                         rn =1\n",
    "                         continue\n",
    "                    else:\n",
    "                        f=0\n",
    "                        break\n",
    "                if j in r02:\n",
    "                    if rn == 2 or rn ==0 :\n",
    "                         rn =2\n",
    "                         continue\n",
    "                    else:\n",
    "                        f=0\n",
    "                        break\n",
    "                if j in r03:\n",
    "                    if rn == 3 or rn ==0 :\n",
    "                         rn =3\n",
    "                         continue\n",
    "                    else:\n",
    "                        f=0\n",
    "                        break\n",
    "            if f:ret.append(i)\n",
    "        return ret\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        one = set(\"qwertyuiopQWERTYUIOP\")\n",
    "        two = set(\"asdfghjklASDFGHJKL\")\n",
    "        three = set(\"zxcvbnmZXCVBNM\")\n",
    "        return list(filter(lambda x: len(set(x) - one) == 0 or len(set(x) - two) == 0 or len(set(x) - three) == 0, words))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        # 遍历，为每一个英文字母标记上键盘对应的行号，然后检测所有字符的行号是否相同\n",
    "        res = []\n",
    "        rowIdx = \"12210111011122000010020202\"\n",
    "        for word in words:\n",
    "            # ord（）函数就是用来返回单个字符的ascii值（0-255）\n",
    "            idx = rowIdx[ord(word[0].lower())-ord('a')]\n",
    "            if all(rowIdx[ord(ch.lower())-ord('a')] == idx for ch in word):\n",
    "                res.append(word)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        row1 = \"qwertyuiop\"\n",
    "        row2 = \"asdfghjkl\"\n",
    "        row3 = \"zxcvbnm\"\n",
    "        output = []\n",
    "        for word in words:\n",
    "            check = True\n",
    "            if word[0].lower() in row1:\n",
    "                cur = 1\n",
    "                while check is True and cur < len(word):\n",
    "                    if word[cur].lower() not in row1:\n",
    "                        check = False    \n",
    "                    cur += 1\n",
    "            elif word[0].lower() in row2:\n",
    "                cur = 1\n",
    "                while check is True and cur < len(word):\n",
    "                    if word[cur].lower() not in row2:\n",
    "                        check = False   \n",
    "                    cur += 1\n",
    "            elif word[0].lower() in row3:\n",
    "                cur = 1\n",
    "                while check is True and cur < len(word):\n",
    "                    if word[cur].lower() not in row3:\n",
    "                        check = False   \n",
    "                    cur += 1\n",
    "            if check == True:\n",
    "                output.append(word)\n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        def sameline(word: str) ->bool:\n",
    "            ln = d[word[0].lower()]\n",
    "            for ch in word[1:].lower():\n",
    "                if ln != d[ch]:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        d = {'q': 0, 'w': 0, 'e': 0, 'r': 0, 't': 0, 'y': 0, 'u': 0, 'i': 0, 'o': 0, 'p': 0,\n",
    "             'a': 1, 's': 1, 'd': 1, 'f': 1, 'g': 1, 'h': 1, 'j': 1, 'k': 1, 'l': 1,\n",
    "             'z': 2, 'x': 2, 'c': 2, 'v': 2, 'b': 2, 'n': 2, 'm': 2}\n",
    "\n",
    "        ret = []\n",
    "        for word in words:\n",
    "            if sameline(word):\n",
    "                ret.append(word)\n",
    "        return ret\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "\n",
    "        str1 = \"qwertyuiop\"\n",
    "        str1 = set(list(str1))\n",
    "        str2 = \"asdfghjkl\"\n",
    "        str2 = set(list(str2))\n",
    "        str3 = \"zxcvbnm\"\n",
    "        str3 = set(list(str3))\n",
    "\n",
    "        output = []\n",
    "        for i in range(len(words)):\n",
    "            temp = words[i]\n",
    "            temp = temp.lower()\n",
    "            if len(set(list(temp))-str1) == 0 or len(set(list(temp))-str2) == 0 or len(set(list(temp)) - str3) == 0:\n",
    "                output.append(words[i])\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        row1 = set(['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'])\n",
    "        row2 = set(['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'])\n",
    "        row3 = set(['z', 'x', 'c', 'v', 'b', 'n', 'm'])\n",
    "        ans = []\n",
    "\n",
    "        for word in words:\n",
    "            temp = set(word.lower())\n",
    "            if temp.issubset(row1) or temp.issubset(row2) or temp.issubset(row3):\n",
    "                ans.append(word)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        for i in words:\n",
    "            j = i.lower()\n",
    "            if j.strip(\"qwertyuiop\") == \"\" or j.strip(\"asdfghjkl\") == \"\" or j.strip(\"zxcvbnm\") == \"\":\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        LINES = [set(\"qwertyuiop\"), set(\"asdfghjkl\"), set(\"zxcvbnm\")]\n",
    "        return [word for word in words if any(set(word.lower()).issubset(LINE) for LINE in LINES)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        keyBoard=['qwertyuiopQWERTYUIOP','asdfghjklASDFGHJKL','zxcvbnmZXCVBNM']\n",
    "        for i in words[:]:\n",
    "            if i[0]in keyBoard[0]:\n",
    "                for j in i[1:]:\n",
    "                    if j not in keyBoard[0]:\n",
    "                        words.remove(i)\n",
    "                        break\n",
    "            elif i[0]in keyBoard[1]:\n",
    "                for j in i[1:]:\n",
    "                    if j not in keyBoard[1]:\n",
    "                        words.remove(i)\n",
    "                        break\n",
    "            else:\n",
    "                for j in i[1:]:\n",
    "                    if j not in keyBoard[2]:\n",
    "                        words.remove(i)\n",
    "                        break\n",
    "        return words\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        set1 = set('qwertyuiop')\n",
    "        set2 = set('asdfghjkl')\n",
    "        set3 = set('zxcvbnm')\n",
    "        res = []\n",
    "        for i in words:\n",
    "            x = i.lower()\n",
    "            setx = set(x)\n",
    "            if setx<=set1 or setx<=set2 or setx<=set3:\n",
    "                res.append(i)\n",
    "               \n",
    "            \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        line1 = set(['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'])\n",
    "        line2 = set(['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'])\n",
    "        line3 = set(['z', 'x', 'c', 'v', 'b', 'n', 'm'])\n",
    "        res = []\n",
    "        for word in words:\n",
    "            line = set(word.lower())\n",
    "            if line.issubset(line1) or line.issubset(line2) or line.issubset(line3):\n",
    "                res.append(word)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        firstRow = \"qwertyuiop\"\n",
    "        secondRow = \"asdfghjkl\"\n",
    "        thirdRow = \"zxcvbnm\"\n",
    "        lookup = dict()\n",
    "        for r, row in enumerate([firstRow, secondRow, thirdRow]):\n",
    "            for c in row:\n",
    "                lookup[c] = r\n",
    "        return [w for w in words if len({lookup[c.lower()] for c in w}) == 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        # lines = [\"ertyuiop\", \"asdfghjkl\", \"zxcvbnm\"]\n",
    "        d = {'q': 0, 'w': 0, 'e': 0, 'r': 0, 't': 0, 'y': 0, 'u': 0, 'i': 0, 'o': 0, 'p': 0,\n",
    "             'a': 1, 's': 1, 'd': 1, 'f': 1, 'g': 1, 'h': 1, 'j': 1, 'k': 1, 'l': 1,\n",
    "             'z': 2, 'x': 2, 'c': 2, 'v': 2, 'b': 2, 'n': 2, 'm': 2}\n",
    "        ret = []\n",
    "        for word in words:\n",
    "            # word = word\n",
    "            fl = d[word[0].lower()]\n",
    "            for ch in word[1:].lower():\n",
    "                if fl != d[ch]:\n",
    "                    fl = -1\n",
    "                    break\n",
    "            if fl != -1:\n",
    "                ret.append(word)\n",
    "        #\n",
    "        return ret\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "            s='12210111011122000010020202' #字母转换成对应的行号\n",
    "            res=[]\n",
    "            for word in words:\n",
    "                word1=word.lower()#转换成小写\n",
    "                t0=s[ord(word1[0])-ord('a')]\n",
    "                f=True\n",
    "                for w in word1:\n",
    "                    t=s[ord(w)-ord('a')]\n",
    "                    if t!=t0: #判断是不是都在同一行\n",
    "                        f=False\n",
    "                        break\n",
    "                if f:\n",
    "                    res.append(word)\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        key_rows = [\"qwertyuiop\", \"asdfghjkl\", \"zxcvbnm\"]\n",
    "        result = []\n",
    "        for word in words:\n",
    "            row, is_line = -1, True # 当前行, 是否同行\n",
    "            for char in word:\n",
    "                if row == -1:  # 第一次判断查询字母所在行\n",
    "                    for i in range(len(key_rows)):\n",
    "                        if char.lower() in key_rows[i]:\n",
    "                            row = i\n",
    "                            break\n",
    "                else:\n",
    "                    if char.lower() not in key_rows[row]:\n",
    "                        is_line = False\n",
    "                        break\n",
    "            if is_line:\n",
    "                result.append(word)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        l1 = 'qwertyuiopQWERTYUIOP'\n",
    "        l2 = 'asdfghjklASDFGHJKL'\n",
    "        l3 = 'zxcvbnmZXCVBNM'\n",
    "        sl, s2, s3 = set(l1), set(l2), set(l3)\n",
    "        res = []\n",
    "        for i in range(len(words)):\n",
    "            s = set(words[i])\n",
    "            if s & sl == s or s & s2 == s or s & s3 == s:\n",
    "                res.append(words[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        ret = []\n",
    "        for word in words:\n",
    "            if self.isSameLine(word.lower()):\n",
    "                ret.append(word)\n",
    "        return ret\n",
    "\n",
    "    def isSameLine(self, word: str) -> bool:\n",
    "        firstline = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']\n",
    "        secondline = ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l']\n",
    "        thirdline = ['z', 'x', 'c', 'v', 'b', 'n', 'm']\n",
    "        line = 0\n",
    "        for i, w in enumerate(word):\n",
    "            if i == 0:\n",
    "                if w in firstline:\n",
    "                    line = 1\n",
    "                elif w in secondline:\n",
    "                    line = 2\n",
    "                else:\n",
    "                    line = 3\n",
    "            else:\n",
    "                if w in firstline:\n",
    "                    temp_line = 1\n",
    "                elif w in secondline:\n",
    "                    temp_line = 2\n",
    "                else:\n",
    "                    temp_line = 3\n",
    "                if line != temp_line:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        l1 = 'qwertyuiopQWERTYUIOP'#创三个字符串\n",
    "        l2 = 'asdfghjklASDFGHJKL'\n",
    "        l3 = 'zxcvbnmZXCVBNM'\n",
    "        l1,l2,l3 = set(l1),set(l2),set(l3)\n",
    "        final = []\n",
    "        for i in range(len(words)):#生成一个单词长度的序列进行循环\n",
    "            s = set(words[i])\n",
    "            if s&l1 == s or s&l2==s or s&l3==s:#逐个比较有没有交集\n",
    "                final.append(words[i])#如果有那就加入到final里面去\n",
    "        return final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        set1 = set(\"qwertyuiop\")\n",
    "        set2 = set(\"asdfghjkl\")\n",
    "        set3 = set(\"zxcvbnm\")\n",
    "        ans = []\n",
    "        for word in words:\n",
    "            if set(word.lower()).issubset(set1) | set(word.lower()).issubset(set2) | set(word.lower()).issubset(set3):\n",
    "                ans.append(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        hash_map = {}\n",
    "        for s in 'qwertyuiop':\n",
    "            hash_map[s] = 1\n",
    "            hash_map[chr(ord(s) - 32)] = 1\n",
    "        for s in 'asdfghjkl':\n",
    "            hash_map[s] = 2\n",
    "            hash_map[chr(ord(s) - 32)] = 2\n",
    "        for s in 'zxcvbnm':\n",
    "            hash_map[s] = 3\n",
    "            hash_map[chr(ord(s) - 32)] = 3\n",
    "        res = []\n",
    "        for word in words:\n",
    "            label = True\n",
    "            index = hash_map[word[0]]\n",
    "            for s in word[1:]:\n",
    "                if hash_map[s] != index:\n",
    "                    label = False\n",
    "                    break\n",
    "            if label:\n",
    "                res.append(word)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        s1,s2,s3='qwertyuiop','asdfghjkl','zxcvbnm'\n",
    "        result=[]\n",
    "        for word in words:\n",
    "            if len(word)==1:\n",
    "                result.append(word)\n",
    "                continue\n",
    "            lowercase_word=word.lower()\n",
    "            s=''\n",
    "            if lowercase_word[0] in s1:\n",
    "                s=s1\n",
    "            elif lowercase_word[0] in s2:\n",
    "                s=s2\n",
    "            else:\n",
    "                s=s3\n",
    "            for i in range(1, len(lowercase_word)):\n",
    "                if lowercase_word[i] in s and i != len(lowercase_word) - 1:\n",
    "                    continue\n",
    "                elif i== len(lowercase_word) - 1 and lowercase_word[i] in s:\n",
    "                    result.append(word)\n",
    "                else:\n",
    "                    break\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        lst = [set(\"qwertyuiopQWERTYUIOP\"), set(\"asdfghjklASDFGHJKL\"), set(\"zxcvbnmZXCVBNM\")]\n",
    "        return [w for w in words if any(set(w) - se == set() for se in lst)]\n",
    "        #ans = []\n",
    "        #for w in words:\n",
    "        #    se = set(w)\n",
    "        #    if se - se1 == set() or se - se2 == set() or se - se3 == set(): ans.append(w)\n",
    "        #return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        rowIdx = \"12210111011122000010020202\"\n",
    "        for word in words:\n",
    "            idx = rowIdx[ord(word[0].lower()) - ord('a')]\n",
    "            if all(rowIdx[ord(ch.lower()) - ord('a')] == idx for ch in word):\n",
    "                ans.append(word)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        s1=set(\"qwertyuiop\")\n",
    "        s2=set(\"asdfghjkl\")\n",
    "        s3=set(\"zxcvbnm\")\n",
    "        res=[]\n",
    "        for s in words:\n",
    "            news=set(s.lower())\n",
    "            if news <= s1 or news <= s2 or news <= s3:\n",
    "                res.append(s)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        row_1 = set(('q','w','e','r','t','y','u','i','o','p'))\n",
    "        row_2 = set(('a','s','d','f','g','h','j','k','l'))\n",
    "        row_3 = set(('z','x','c','v','b','n','m'))\n",
    "        ans = []\n",
    "\n",
    "        for i in words:\n",
    "            cur = set()\n",
    "            for j in i:\n",
    "                cur.update(j.lower())\n",
    "            if cur.difference(row_1) == set():\n",
    "                ans.append(i)\n",
    "            elif cur.difference(row_2) == set():\n",
    "                ans.append(i)\n",
    "            elif cur.difference(row_3) == set():\n",
    "                ans.append(i)\n",
    "        return ans\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        s1 = \"qwertyuiop\"\n",
    "        s2 = \"asdfghjkl\"\n",
    "        s3 = \"zxcvbnm\"\n",
    "        l = []\n",
    "\n",
    "        def search(string,s):\n",
    "            idx = 0\n",
    "            for i in range(len(string)):\n",
    "                if string[i].lower() in s:\n",
    "                    idx+=1\n",
    "                    continue\n",
    "                else:\n",
    "                    break\n",
    "            if idx == len(string):\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "\n",
    "        for i in words:\n",
    "            if search(i,s1) or search(i,s2) or search(i,s3):\n",
    "                l.append(i)\n",
    "\n",
    "        return l\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        d={\n",
    "        1:'qwertyuiop',\n",
    "        2:'asdfghjkl',\n",
    "        3:'zxcvbnm'\n",
    "        }\n",
    "        ans=[]\n",
    "        tmp=set()\n",
    "        for word in words:\n",
    "            for ch in word:\n",
    "                if ch.lower() in d[1]:\n",
    "                    tmp.add(1)\n",
    "                elif ch.lower() in d[2]:\n",
    "                    tmp.add(2)\n",
    "                elif ch.lower() in d[3]:\n",
    "                    tmp.add(3)\n",
    "            if len(tmp)==1:\n",
    "                t=tmp.pop()\n",
    "                ans.append(word)\n",
    "            else:\n",
    "                tmp=set()\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, w: List[str]) -> List[str]:\n",
    "        l1=\"qwertyuiop\"\n",
    "        l2=\"asdfghjkl\"\n",
    "        l3= \"zxcvbnm\"\n",
    "        res=[]\n",
    "        for i in w:\n",
    "            l=i.lower()\n",
    "            t=l[0]\n",
    "            flag=True\n",
    "            if t in l1:\n",
    "                for k in l:\n",
    "                    if k not in  l1:\n",
    "                        flag=False\n",
    "                        break\n",
    "                if flag:\n",
    "                    res.append(i)\n",
    "            elif t in l2:\n",
    "                for k in l:\n",
    "                    if k not in  l2:\n",
    "                        flag=False\n",
    "                        break\n",
    "                if flag:\n",
    "                    res.append(i)\n",
    "            else:\n",
    "                for k in l:\n",
    "                    if k not in  l3:\n",
    "                        flag=False\n",
    "                        break\n",
    "                if flag:\n",
    "                    res.append(i)\n",
    "        return res\n",
    "\t\t\t\t\t\t\n",
    "                        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        for i in words:\n",
    "            j = i.lower()\n",
    "            if j.strip(\"qwertyuiop\") == \"\" or j.strip(\"asdfghjkl\") == \"\" or j.strip(\"zxcvbnm\") == \"\":\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        first = ['q','w','e','r','t','y','u','i','o','p']\n",
    "        second = ['a','s','d','f','g','h','j','k','l']\n",
    "        third = ['z','x','c','v','b','n','m']\n",
    "        result = []\n",
    "        first,second,third = set(first),set(second),set(third)\n",
    "        for word in words:\n",
    "            if first.intersection(set(word.lower())) == set(word.lower()):\n",
    "                print(set(word.lower()))\n",
    "                result.append(word)\n",
    "            elif second.intersection(set(word.lower())) == set(word.lower()):\n",
    "                result.append(word)\n",
    "            elif third.intersection(set(word.lower())) == set(word.lower()): \n",
    "                result.append(word)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        \"\"\"\n",
    "        两层循环\n",
    "        \"\"\"\n",
    "        ans = []\n",
    "        for word in words:\n",
    "            jug = []\n",
    "            for _ in word:\n",
    "                if _ in \"qwertyuiopQWERTYUIOP\":\n",
    "                    jug.append(1)\n",
    "                if _ in \"asdfghjklASDFGHJKL\":\n",
    "                    jug.append(2)                \n",
    "                if _ in \"zxcvbnmZXCVBNM\":\n",
    "                    jug.append(3)\n",
    "            if len(set(jug)) == 1:\n",
    "                ans.append(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        str1 = \"qwertyuiop\"\n",
    "        str2 = \"asdfghjkl\"\n",
    "        str3 = \"zxcvbnm\"\n",
    "        for word in words:\n",
    "            mstr = str1\n",
    "            if word[0].lower() in str2:\n",
    "                mstr = str2\n",
    "            if word[0].lower() in str3:\n",
    "                mstr = str3\n",
    "            flag = True\n",
    "            for ch in word:\n",
    "                if ch.lower() not in mstr:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                res.append(word)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        for i in words:\n",
    "            first = i[0].lower()\n",
    "            row = \"qwertyuiop\" if first in \"qwertyuiop\" else \"asdfghjkl\" if first in \"asdfghjkl\" else \"zxcvbnm\"\n",
    "            for j in i[1:]:\n",
    "                if j.lower() not in row:\n",
    "                    break \n",
    "            else:\n",
    "                res.append(i)\n",
    "        return res \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        judging_list = []\n",
    "        res = []\n",
    "        for i in range(0,26):\n",
    "            judging_list.append(0)\n",
    "        for char1 in 'qwertyuiop':\n",
    "            judging_list[ord(char1)-97] = 1\n",
    "        for char2 in 'asdfghjkl':\n",
    "            judging_list[ord(char2)-97] = 2\n",
    "        for char3 in 'zxcvbnm':\n",
    "            judging_list[ord(char3)-97] = 3\n",
    "        for i in range(0,len(words)):\n",
    "            corr = True\n",
    "            first_char = words[i][0]\n",
    "            first_ascii = ord(first_char)\n",
    "            if first_ascii < 91:\n",
    "                first_ascii += 32\n",
    "            for char in words[i]:\n",
    "                ascii = ord(char)\n",
    "                if ascii < 91 :\n",
    "                    ascii += 32\n",
    "                if judging_list[ascii-97] != judging_list[first_ascii-97]:\n",
    "                    corr =False\n",
    "            if corr:\n",
    "                res.append(words[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        chars = set(\"qwertyuiop\"+\"qwertyuiop\".upper() ), set(\"asdfghjkl\"+\"asdfghjkl\".upper()), set(\"zxcvbnm\"+\"zxcvbnm\".upper() )\n",
    "        ret = []\n",
    "        for word in words:\n",
    "            for char in chars:\n",
    "                if set(word) & char == set(word):\n",
    "                    ret.append(word)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        s1=\"qwertyuiop\"\n",
    "        s2=\"asdfghjkl\"\n",
    "        s3=\"zxcvbnm\"\n",
    "        list1=[]\n",
    "        for i in words:\n",
    "            num=[]\n",
    "            for j in i.lower():\n",
    "                if(j in s1):\n",
    "                    num.append(1)\n",
    "                if(j in s2):\n",
    "                    num.append(2)\n",
    "                if(j in s3):\n",
    "                    num.append(3)\n",
    "            #print(num)\n",
    "            if(len(set(num))==1):\n",
    "                list1.append(i)\n",
    "        return list1        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        ret,dic=[],[\"qwertyuiop\",\"asdfghjkl\",\"zxcvbnm\"]\n",
    "        flag=False\n",
    "        for word in words:\n",
    "            for i in dic:\n",
    "                for alpha in word.lower():\n",
    "                    if not alpha in i:\n",
    "                        flag=True\n",
    "                        break\n",
    "                if flag:\n",
    "                    flag=False\n",
    "                    continue\n",
    "                ret.append(word)\n",
    "                break\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        res=[]\n",
    "\n",
    "        #通过循环在words中取得每一个单词\n",
    "        for i in words:\n",
    "            #由于有大小写之分，创建一个变量来存储单词的小写\n",
    "            j=i.lower()\n",
    "\n",
    "            #用strip()函数对单词进行判断，把所有字符替换成\"（空），如果满足则在结果中添加单词\n",
    "            if j.strip('qwertyuiop')=='' or j.strip('asdfghjkl')=='' or j.strip('zxcvbnm')=='':\n",
    "                res.append(i)\n",
    "        \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "FIRST=\"qwertyuiop\"\n",
    "SEC=\"asdfghjkl\"\n",
    "THIRD=\"zxcvbnm\"\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        res=[]\n",
    "        for word in words:\n",
    "            wl=word.lower()\n",
    "            if wl.strip('qwertyuiop')=='' or wl.strip('asdfghjkl')=='' or wl.strip('zxcvbnm')=='':\n",
    "                res.append(word)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "d = {}\n",
    "for i, w in enumerate((\"qwertyuiop\", \"asdfghjkl\", \"zxcvbnm\")):\n",
    "    d.update(product(w, (i,)))\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        for w in words:\n",
    "            first = d[w[0].lower()]\n",
    "            if all(d[c] == first for c in w.lower()):\n",
    "                ans.append(w)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        s1 = 'qwertyuiop'\n",
    "        s2 = 'asdfghjkl'\n",
    "        s3 = 'zxcvbnm'\n",
    "        dict1 = {c: i for i, c in enumerate(s1)}\n",
    "        dict2 = {c: i for i, c in enumerate(s2)}\n",
    "        dict3 = {c: i for i, c in enumerate(s3)}\n",
    "        temp_dict = {}\n",
    "        for j, item in enumerate(words):\n",
    "            if (item[0] in dict1.keys()) or (item[0].isupper() and item[0].lower() in dict1.keys()):\n",
    "                temp_dict = dict1\n",
    "            elif (item[0] in dict2.keys()) or (item[0].isupper() and item[0].lower() in dict2.keys()):\n",
    "                temp_dict = dict2\n",
    "            else:\n",
    "                temp_dict = dict3\n",
    "            for i in range(1, len(item)):\n",
    "                if item[i].isupper():\n",
    "                    if item[i].lower() not in temp_dict.keys():\n",
    "                        words[j] = ''\n",
    "                        break\n",
    "                else:\n",
    "                    if item[i] not in temp_dict.keys():\n",
    "                        words[j] = ''\n",
    "                        break\n",
    "\n",
    "        while('' in words):\n",
    "            words.remove('')\n",
    "        return words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        rows = [\"qwertyuiop\", \"asdfghjkl\", \"zxcvbnm\"]\n",
    "        ans = []\n",
    "        for word in words:\n",
    "            for row in rows:\n",
    "                flag = True\n",
    "                for c in word:\n",
    "                    c = c.lower()\n",
    "                    if c not in row:\n",
    "                        flag = False\n",
    "                        break\n",
    "                if flag:\n",
    "                    ans.append(word)\n",
    "                    break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findWords(self, words: List[str]) -> List[str]:\r\n",
    "        # 字典\r\n",
    "        dict1 = {\"z\":1,\"x\":1,\"c\":1,\"v\":1,\"b\":1,\"n\":1,\"m\":1\r\n",
    "                 ,\"a\":2, \"s\":2, \"d\":2, \"f\":2, \"g\":2, \"h\":2, \"j\":2, \"k\":2, \"l\":2\r\n",
    "                ,\"q\":3, \"w\":3, \"e\":3, \"r\":3, \"t\":3, \"y\":3, \"u\":3, \"i\":3, \"o\":3, \"p\":3}\r\n",
    "        samewords=[]\r\n",
    "        i = 0\r\n",
    "        while i < len(words):\r\n",
    "            # if len(words[i]) == 1:\r\n",
    "            #     samewords.append(words[i])\r\n",
    "            #     i += 1\r\n",
    "            #     continue\r\n",
    "            # 首字母行号\r\n",
    "            firwor = dict1[words[i][0].lower()]\r\n",
    "            issame=True\r\n",
    "            for _ in words[i]:\r\n",
    "                if dict1[_.lower()] != firwor:\r\n",
    "                    issame=False\r\n",
    "                    break\r\n",
    "            if issame==True:\r\n",
    "                samewords.append(words[i])\r\n",
    "            i += 1\r\n",
    "\r\n",
    "        return samewords\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        # key_map = { s:idx+1 for idx, keys in enumerate([\"qwertyuiop\", \"asdfghjkl\", \"zxcvbnm\"]) for s in keys}\n",
    "        key_map = {'q': 1, 'w': 1, 'e': 1, 'r': 1, 't': 1, 'y': 1, 'u': 1, 'i': 1, 'o': 1, 'p': 1, 'a': 2, 's': 2, 'd': 2, 'f': 2, 'g': 2, 'h': 2, 'j': 2, 'k': 2, 'l': 2, 'z': 3, 'x': 3, 'c': 3, 'v': 3, 'b': 3, 'n': 3, 'm': 3}\n",
    "        res = []\n",
    "        for word in words:\n",
    "            tag = key_map[word[0].lower()]\n",
    "            flag = True\n",
    "            for s in word.lower():\n",
    "                if key_map[s] != tag:\n",
    "                    flag = False\n",
    "                    continue\n",
    "            if flag:\n",
    "                res.append(word)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        b=[]\n",
    "        m=\"qwertyuiop\"\n",
    "        n=\"asdfghjkl\"\n",
    "        j=\"zxcvbnm\"\n",
    "        for i in words:\n",
    "            g=i.lower()\n",
    "            a={'m':0,'n':0,'j':0}\n",
    "            for k in g:\n",
    "                if k in m:\n",
    "                    a['m']+=1\n",
    "                elif k in n:\n",
    "                    a['n']+=1\n",
    "                else:\n",
    "                    a['j']+=1\n",
    "            if a['m']==0 and a['n']==0 or a['m']==0 and a['j']==0 or a['n']==0 and a['j']==0:\n",
    "                b.append(i)\n",
    "        return b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "\n",
    "\n",
    "        first = \"qwertyuiop\"\n",
    "        second = \"asdfghjkl\"\n",
    "        three = \"zxcvbnm\"\n",
    "        sub_str_list = [first, second, three]\n",
    "\n",
    "        res = []\n",
    "\n",
    "        for word_source in words:\n",
    "            word = word_source.lower()\n",
    "            first_w = word[0]\n",
    "            for sub_str in sub_str_list:\n",
    "                if first_w in sub_str:\n",
    "                    flag = True\n",
    "                    for others in word[1:]:\n",
    "                        if others not in sub_str:\n",
    "                            flag = False\n",
    "                            break\n",
    "\n",
    "                    if flag:\n",
    "                        res.append(word_source)\n",
    "\n",
    "                    break\n",
    "\n",
    "\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    words = [\"Hello\", \"Alaska\", \"Dad\", \"Peace\"]\n",
    "    print(Solution().findWords(words))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        r=[set('qwertyuiop'),set('asdfghjkl'),set('zxcvbnm')]\n",
    "        result=[]\n",
    "        for word in words:\n",
    "            w=word.lower()\n",
    "            for i in range(3):\n",
    "                if w[0] in r[i]:\n",
    "                    f=True\n",
    "                    for j in range(1,len(w)):\n",
    "                        if w[j] not in r[i]:\n",
    "                            f=False\n",
    "                            break\n",
    "                    if f:\n",
    "                        result.append(word)\n",
    "                    break\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        sec_1 = \"qwertyuiop\"\n",
    "        sec_2 = \"asdfghjkl\"\n",
    "        sec_3 = \"zxcvbnm\"\n",
    "        res = []\n",
    "        for word in words:\n",
    "            cnt1=cnt2=cnt3=0\n",
    "            for c in list(word.lower()):\n",
    "                if c in sec_1: cnt1 += 1\n",
    "                if c in sec_2: cnt2 += 1\n",
    "                if c in sec_3: cnt3 += 1\n",
    "            if len(word) == cnt1 or len(word) == cnt2 or len(word) == cnt3:\n",
    "                res.append(word)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        jianpan1 = 'qwertyuiopQWERTYUIOP'\n",
    "        jianpan2 = 'asdfghjklASDFGHJKL'\n",
    "        jianpan3 = 'zxcvbnmZXCVBNM'\n",
    "        ans=[]\n",
    "        for word in words:\n",
    "            if all(i in jianpan1 for i in word) or all(i in jianpan2 for i in word) or all(i in jianpan3 for i in word):\n",
    "                ans.append(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findWords(self, words: List[str]) -> List[str]:\r\n",
    "        # 字典\r\n",
    "        dict1 = [\"z\", \"x\", \"c\", \"v\", \"b\", \"n\", \"m\"]\r\n",
    "        dict2 = [\"a\", \"s\", \"d\", \"f\", \"g\", \"h\", \"j\", \"k\", \"l\"]\r\n",
    "        # dict3 = [\"q\", \"w\", \"e\", \"r\", \"t\", \"y\", \"u\", \"i\", \"o\", \"p\"]\r\n",
    "        \r\n",
    "        i=0\r\n",
    "        while i < len(words):\r\n",
    "            if len(words[i])==1:\r\n",
    "                i+=1\r\n",
    "                continue\r\n",
    "            # 小写           \r\n",
    "            li = list(words[i].lower())\r\n",
    "            if li[0] in dict1:\r\n",
    "                for _ in li:\r\n",
    "                    if _ not in dict1:# 是否同一行\r\n",
    "                        words.remove(words[i])\r\n",
    "                        i-=1\r\n",
    "                        break\r\n",
    "\r\n",
    "            elif li[0] in dict2:\r\n",
    "                for _ in li:\r\n",
    "                    if _ not in dict2:\r\n",
    "                        words.remove(words[i])\r\n",
    "                        i-=1\r\n",
    "                        break\r\n",
    "\r\n",
    "            else:\r\n",
    "                for _ in li:\r\n",
    "                    if _ in dict1 or _ in dict2:\r\n",
    "                        words.remove(words[i])\r\n",
    "                        i-=1\r\n",
    "                        break\r\n",
    "            i+=1\r\n",
    "        \r\n",
    "        return words\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        set1 = set(\"qwertyuiop\")\n",
    "        set2 = set(\"asdfghjkl\")\n",
    "        set3 = set(\"zxcvbnm\")\n",
    "        res = []\n",
    "        for s in words:\n",
    "            temp = set(s.lower())\n",
    "            if temp.issubset(set1) or temp.issubset(set2) or temp.issubset(set3):\n",
    "                res.append(s)\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        b=[]\n",
    "        m=\"qwertyuiop\"\n",
    "        n=\"asdfghjkl\"\n",
    "        j=\"zxcvbnm\"\n",
    "        for i in words:\n",
    "            g=i.lower()\n",
    "            a={'m':0,'n':0,'j':0}\n",
    "            for k in g:\n",
    "                if k in m:\n",
    "                    a['m']+=1\n",
    "                elif k in n:\n",
    "                    a['n']+=1\n",
    "                else:\n",
    "                    a['j']+=1\n",
    "            if a['m']==0 and a['n']==0 or a['m']==0 and a['j']==0 or a['n']==0 and a['j']==0:\n",
    "                b.append(i)\n",
    "        return b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        str1=set('qwertyuiop')\n",
    "        str2=set('asdfghjkl')\n",
    "        str3=set('zxcvbnm')\n",
    "        out=[]\n",
    "        for i in words:\n",
    "            x=set(i.lower())\n",
    "            if x<=str1 or x<=str2 or x<=str3:\n",
    "                out.append(i)\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        rowid = \"12210111011122000010020202\"\n",
    "        for word in words:\n",
    "            idx = rowid[ord(word[0].lower())-ord('a')]\n",
    "            if all(rowid[ord(ch.lower())-ord('a')] == idx for ch in word):\n",
    "                ans.append(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        s1=set('qwertyuiop')\n",
    "        s2=set('asdfghjkl')\n",
    "        s3=set('zxcvbnm')\n",
    "        ans=[]\n",
    "        for i in words:\n",
    "            s=set(i.lower())\n",
    "            if s&s1==s or s&s2==s or s&s3==s:\n",
    "                ans.append(i)\n",
    "        return ans\n",
    "            \n",
    "                \n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        l1 = \"qwertyuiop\"\n",
    "        l2 = \"asdfghjkl\"\n",
    "        l3 = \"zxcvbnm\"\n",
    "        words = words\n",
    "        res = []\n",
    "        \n",
    "        for word1 in words:\n",
    "            flag = False\n",
    "            word = word1.lower()\n",
    "            if word[0] in l1:\n",
    "                for w in word:\n",
    "                    if w not in l1:\n",
    "                        flag = True\n",
    "                        break\n",
    "                if not flag:\n",
    "                    res.append(word1)\n",
    "            if word[0] in l2:\n",
    "                for w in word:\n",
    "                    if w not in l2:\n",
    "                        flag = True\n",
    "                        break\n",
    "                if not flag:\n",
    "                    res.append(word1)\n",
    "            if word[0] in l3:\n",
    "                for w in word:\n",
    "                    if w not in l3:\n",
    "                        flag = True\n",
    "                        break\n",
    "                if not flag:\n",
    "                    res.append(word1)\n",
    "\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def findWords(self, words: List[str]) -> List[str]:\n",
    "#         test1=\"qwertyuiop\"\n",
    "#         test2=\"asdfghjkl\"\n",
    "#         test3=\"zxcvbnm\"\n",
    "#         for x in words:\n",
    "#             for s in x:\n",
    "        \n",
    "# class Solution:\n",
    "#     def findWords(self, words: List[str]) -> List[str]:\n",
    "#         res=[]\n",
    "\n",
    "#         #通过循环在words中取得每一个单词\n",
    "#         for i in words:\n",
    "#             #由于有大小写之分，创建一个变量来存储单词的小写\n",
    "#             j=i.lower()\n",
    "\n",
    "#             #用strip()函数对单词进行判断，把所有字符替换成\"（空），如果满足则在结果中添加单词\n",
    "#             if j.strip('qwertyuiop')=='' or j.strip('asdfghjkl')=='' or j.strip('zxcvbnm')=='':\n",
    "#                 res.append(i)\n",
    "        \n",
    "#         return res\n",
    "\n",
    "class Solution:\n",
    "    def findWords(self, words: List[str]) -> List[str]:\n",
    "        out = []\n",
    "        s1 = \"qwertyuiop\"\n",
    "        s2 = \"asdfghjkl\"\n",
    "        s3 = \"zxcvbnm\"\n",
    "        for word in words:\n",
    "            if all(x.lower() in s1 for x in word) or all(x.lower() in s2 for x in word) or all(x.lower() in s3 for x in word):\n",
    "                out.append(word)\n",
    "        return out"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
